package com.roc.steelend.controller;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.roc.steelend.controller.vo.reconciliation.FindReconciliationReception;
import com.roc.steelend.controller.vo.reconciliation.ReconciliationBuyReception;
import com.roc.steelend.controller.vo.reconciliation.ReconciliationKaipingReception;
import com.roc.steelend.controller.vo.reconciliation.ReconciliationSaleReception;
import com.roc.steelend.controller.vo.returnformat.ReturnMsg;
import com.roc.steelend.controller.vo.returnformat.ReturnMsgForTable;
import com.roc.steelend.pojo.ReconciliationBuy;
import com.roc.steelend.pojo.ReconciliationKaiping;
import com.roc.steelend.pojo.ReconciliationSale;
import com.roc.steelend.service.KaipingOrderService;
import com.roc.steelend.service.ReconciliationBuyService;
import com.roc.steelend.service.ReconciliationKaipingService;
import com.roc.steelend.service.ReconciliationSaleService;
import com.roc.steelend.util.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.jws.Oneway;
import javax.servlet.http.HttpServletRequest;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;

/**
 * 调账控制层
 */
@CrossOrigin
@RestController
@Api(tags = "调账功能接口")
public class ReconciliationController {


    @Autowired
    private ReconciliationSaleService reconciliationSaleService;
    @Autowired
    private ReconciliationBuyService reconciliationBuyService;
    @Autowired
    private ReconciliationKaipingService reconciliationKaipingService;

       @PostMapping("/v1/post/reconciliation/buy")
        public ReturnMsg  postReconciliationBuy(@RequestBody ReconciliationBuyReception reconciliationBuyReception, HttpServletRequest request){
           String token = request.getHeader("token");
           DecodedJWT decodedJWT = JWTUtil.getToken(token);
           String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
           return  reconciliationBuyService.postReconciliationBuy(reconciliationBuyReception,Integer.valueOf(employeeIdAsString));

       }

    @PostMapping("/v1/post/reconciliation/sale")
    public ReturnMsg  postReconciliationSale(@RequestBody ReconciliationSaleReception reconciliationSaleReception, HttpServletRequest request){
           String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return reconciliationSaleService.postReconciliationSale(reconciliationSaleReception,Integer.valueOf(employeeIdAsString));


    }



    @PostMapping("/v1/post/reconciliation/kaiping")
    public ReturnMsg  postReconciliationKaiping(@RequestBody ReconciliationKaipingReception reconciliationKaipingReception){
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String token = request.getHeader("token");
        DecodedJWT decodedJWT = JWTUtil.getToken(token);
        String employeeIdAsString = decodedJWT.getClaim("employeeId").asString();
        return reconciliationKaipingService.postReconciliationKaiping(reconciliationKaipingReception,Integer.valueOf(employeeIdAsString));
    }


    @PostMapping("/v1/find/reconciliation/data")
    @ApiOperation(value = "调账查询接口(ps:特殊情况使用post,但不修改数据!!!)", notes = "调账查询接口(ps:特殊情况使用post,但不修改数据!!!)")
    public ReturnMsgForTable findReconciliationData(@RequestBody FindReconciliationReception findReconciliationReception) throws InterruptedException {

        ReturnMsgForTable returnMsgForTable = new ReturnMsgForTable();

        if (findReconciliationReception.getReconciliationType()==1){
//采购
            returnMsgForTable.setCurrenPage(findReconciliationReception.getCurrentPage());
            returnMsgForTable.setObjectList(reconciliationBuyService.findReconciliationDataByBuy(findReconciliationReception));
            returnMsgForTable.setTotal(reconciliationBuyService.findReconciliationDataByBuyCount(findReconciliationReception));
        }else if(findReconciliationReception.getReconciliationType()==2){
//销售
            returnMsgForTable.setCurrenPage(findReconciliationReception.getCurrentPage());
            returnMsgForTable.setObjectList(reconciliationSaleService.findReconciliationDataBySale(findReconciliationReception));
            returnMsgForTable.setTotal(reconciliationSaleService.findReconciliationDataBySaleCount(findReconciliationReception));
        }else if (findReconciliationReception.getReconciliationType()==3){
//开平
            returnMsgForTable.setCurrenPage(findReconciliationReception.getCurrentPage());
            returnMsgForTable.setObjectList(reconciliationKaipingService.findReconciliationDataByKaiping(findReconciliationReception));
            returnMsgForTable.setTotal(reconciliationKaipingService.findReconciliationDataByKaipingCount(findReconciliationReception));
        }


        return  returnMsgForTable;
    }

    /**
     * 采购调账细节查询
     */
    @GetMapping("/v1/buy/reconciliation/detail/select")
    public List<ReconciliationBuy> buyReconciliationDetailSelect(@RequestParam("reconciliationId")BigInteger reconciliationId){

        return reconciliationBuyService.buyReconciliationDetailSelect(reconciliationId);
    }



    /**
     * 销售调账细节查询
     */
    @GetMapping("/v1/sale/reconciliation/detail/select")
    public List<ReconciliationSale> saleReconciliationDetailSelect(@RequestParam("reconciliationId")BigInteger reconciliationId){

        return reconciliationSaleService.saleReconciliationDetailSelect(reconciliationId);
    }


    /**
     * 开平调账细节查询
     */
    @GetMapping("/v1/kaiping/reconciliation/detail/select")
    public List<ReconciliationKaiping> kaipingReconciliationDetailSelect(@RequestParam("reconciliationId")BigInteger reconciliationId){

        return reconciliationKaipingService.kaipingReconciliationDetailSelect(reconciliationId);
    }
}
